<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="common/include :: header('服务项列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="post-form">
                <div class="select-list">
                    <ul>
                        <li>
                            名称：<input type="text" name="name"/>
                        </li>
                        <li>
                            是否套餐：
                            <select name="carPackage" th:with="type=${@dict.getType('si_car_package')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            服务分类：
                            <select name="serviceCatalog" th:with="type=${@dict.getType('si_service_catalog')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            审核状态：
                            <select name="auditStatus" th:with="type=${@dict.getType('si_audit_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            上架状态：
                            <select name="saleStatus" th:with="type=${@dict.getType('si_sale_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:post:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-warning auditBtn disabled" id="startAudit" onclick="startAudit()">
                <i class="fa fa-download"></i> 发起审核
            </a>

        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="common/include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:post:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:post:remove')}]];
    var serviceCatalog = [[${@dict.getType('si_service_catalog')}]];//服务分类
    var saleStatus = [[${@dict.getType('si_sale_status')}]];	//上架状态
    var auditStatus = [[${@dict.getType('si_audit_status')}]];	//审核状态
    var carPackage = [[${@dict.getType('si_car_package')}]];//是否套餐


    //点击审核进行跳转
    function startAudit() {
        var rows = $('#bootstrap-table').bootstrapTable('getSelections');
        var row = rows[0];
        var id = row.id;
        $.modal.open("发起审核", "/serviceItem/startAuditPage?id=" + id)
    }


    var prefix = "/serviceItem";
    $(function () {
        var options = {
            url: prefix + "/query",
            createUrl: prefix + "/addPage",
            updateUrl: prefix + "/editPage?id={id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "养修",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '服务项id',
                    visible: false
                },
                {
                    field: 'name',
                    title: '服务项名称',
                    sortable: true
                },

                {
                    field: 'originalPrice',
                    title: '服务项原价',
                    sortable: true
                },
                {
                    field: 'discountPrice',
                    title: '服务项折扣价',
                    sortable: true
                },
                {
                    field: 'carPackage',
                    title: '是否套餐',
                    align: 'center',
                    formatter: function (value, row, index) {
                        // return $.table.selectDictLabel(datas4, value);
                        if (value) {
                            return "是";
                        } else {
                            return "否";
                        }
                    }
                },
                {
                    field: 'info',
                    title: '备注信息',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 10, "open");
                    }
                },
                {
                    field: 'serviceCatalog',
                    title: '服务分类',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(serviceCatalog, value);
                    }
                },
                {
                    field: 'auditStatus',
                    title: '审核状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(auditStatus, value);
                    }
                },
                {
                    field: 'saleStatus',
                    title: '上架状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(saleStatus, value);
                    }
                },


                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        var ar;
                        if (row.auditStatus == 1 || row.saleStatus == 1) {
                            ar = "disabled";
                        }
                        actions.push('<a  class="btn btn-success btn-xs ' + ar + '"  href="javascript:void(0)" ' +
                            'onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');

                        if (row.saleStatus != 1) {
                            var arr;
                            if (row.carPackage && row.auditStatus != 2) {
                                arr = "disabled";
                            }
                            actions.push('<a  class="btn btn-info btn-xs  ' + arr + '" href="javascript:void(0)"' +
                                ' onclick="saleOn(\'' + row.id + '\')"><i class="fa fa-remove"></i>上架</a>');
                        }

                        if (row.saleStatus == 1) {
                            actions.push('<a  class="btn btn-danger btn-xs " href="javascript:void(0)"' +
                                ' onclick="saleOff(\'' + row.id + '\')"><i class="fa fa-remove"></i>下架</a>');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    //发起审核
    $("#bootstrap-table").on("check.bs.table uncheck.bs.table check-all.bs.table " +
        "uncheck-all.bs.table load-success.bs.table", function () {

        var rows = $('#bootstrap-table').bootstrapTable('getSelections');
        var row = rows[0];
        console.log(row)
        $('#startAudit').addClass('disabled');
        if (row) {
            if (rows.length == 1) {
                if (row.carPackage) {//是否是套餐
                    console.log(row.carPackage)
                    if (row.saleStatus == 0) {
                        if (row.auditStatus == 0 || row.auditStatus == 3) {//是否是初始化或者重新调整
                            console.log(row.auditStatus)
                            $('#startAudit').removeClass('disabled');
                            return;
                        }
                    }
                }
            }
        }
    })

    //上架
    function saleOn(id) {
        $.modal.confirm("确认需要上架此服务项吗?", function () {
            $.operate.submit(prefix + "/saleOn?id=" + id, "post");
        });
    }

    //下架
    function saleOff(id) {
        $.modal.confirm("确认需要下架此服务项吗?", function () {
            $.operate.submit(prefix + "/saleOff?id=" + id, "post");
        });
    }
</script>
</body>
</html>